﻿Public Class Productos
    Public variable As Integer = 0
    Public equivalencia As Integer = 0
    Public otro As Integer = 0
    Public Sub Mostrar()
        If guardar.Enabled = False Then
            guardar.Enabled = True
            cancelar.Enabled = True
            agregar.Enabled = False
            modificar.Enabled = False
            eliminar.Enabled = False
            imprimir.Enabled = False
            PanelDetalle.Enabled = False
            PanelPrincipal.Enabled = True
        Else
            cancelar.Enabled = False
            guardar.Enabled = False
            agregar.Enabled = True
            modificar.Enabled = True
            eliminar.Enabled = True
            imprimir.Enabled = True
            PanelDetalle.Enabled = True
            PanelPrincipal.Enabled = False

        End If
    End Sub
    Public Sub vacio()
        IdbodegaLookUpEdit.Text = ""
        IdproductoscategoriaLookUpEdit.Text = ""
        IdproductoTextEdit.Text = ""
        IdunidadescategoriaLookUpEdit.Text = ""
        DescripcionTextEdit.Text = ""
        ObservacionTextEdit.Text = ""
        PreciocomprauniTextEdit.EditValue = 0
        ExistenciaTextEdit.EditValue = 0
    End Sub
    Private Sub ProductosBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ProductosBindingNavigatorSaveItem.Click
        Try
            Me.Validate()
            Me.ProductosBindingSource.EndEdit()
            Me.TableAdapterManager.UpdateAll(Me.DbFireAndIceDataSet)
        Catch ex As Exception
            AlertControl1.AppearanceCaption.ForeColor = Color.Red
            AlertControl1.Show(Owner, "ERROR", "No se pudo guardar registros de Productos")
        End Try
    End Sub
    Private Sub Productos_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        SimpleButton4.Visible = True

        Try
            'TODO: This line of code loads data into the 'DbFireAndIceDataSet.unidadescategoria' table. You can move, or remove it, as needed.
            Me.UnidadescategoriaTableAdapter.Fill(Me.DbFireAndIceDataSet.unidadescategoria)
        Catch ex As Exception
            AlertControl1.AppearanceCaption.ForeColor = Color.Red
            AlertControl1.Show(Owner, "ERROR", "No se pudo cargar registros de Categoría Unidades")
        End Try

        Try
            'TODO: This line of code loads data into the 'DbFireAndIceDataSet.bodega' table. You can move, or remove it, as needed.
            Me.BodegaTableAdapter.Fill(Me.DbFireAndIceDataSet.bodega)
        Catch ex As Exception
            AlertControl1.AppearanceCaption.ForeColor = Color.Red
            AlertControl1.Show(Owner, "ERROR", "No se pudo cargar registros de Bodegas")

        End Try

        Try
            'TODO: This line of code loads data into the 'DbFireAndIceDataSet.productoscategoria' table. You can move, or remove it, as needed.
            Me.ProductoscategoriaTableAdapter.Fill(Me.DbFireAndIceDataSet.productoscategoria)
        Catch ex As Exception
            AlertControl1.AppearanceCaption.ForeColor = Color.Red
            AlertControl1.Show(Owner, "ERROR", "No se pudo cargar registros de Categoría Productos")

        End Try

        Try
            'TODO: This line of code loads data into the 'DbFireAndIceDataSet.productos' table. You can move, or remove it, as needed.
            Me.ProductosTableAdapter.Fill(Me.DbFireAndIceDataSet.productos)
        Catch ex As Exception
            AlertControl1.AppearanceCaption.ForeColor = Color.Red
            AlertControl1.Show(Owner, "ERROR", "No se pudo cargar registros de Productos")

        End Try
        ExistenciaTextEdit.Properties.ReadOnly = True
        PreciocomprauniTextEdit.Properties.ReadOnly = True

        PanelDetalle.Enabled = True
        PanelPrincipal.Enabled = False
        PrecioVentaCliente.EditValue = 0.0
        PrecioVentaVisita.EditValue = 0.0
        PreciocomprauniTextEdit2.EditValue = 0.0
        Idtipo.EditValue = 0
    End Sub
    Private Sub agregar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles agregar.Click

        otro = 1
        PrecioVentaCliente.EditValue = 0.0
        PrecioVentaVisita.EditValue = 0.0
        PreciocomprauniTextEdit2.EditValue = 0.0
        Idtipo.EditValue = 0
        ExistenciaTextEdit2.Visible = False
        ExistenciaTextEdit.Visible = True
        PreciocomprauniTextEdit.Visible = True
        PreciocomprauniTextEdit2.Visible = False
        variable = 1
        BindingNavigatorAddNewItem.PerformClick()
        Mostrar()
        ExistenciaTextEdit.EditValue = 0
        CheckEdit1.Checked = True

        AgregarInvetario.Enabled = False
        IdunidadescategoriaLookUpEdit.Enabled = True
        ExistenciaTextEdit.Properties.ReadOnly = True
        PreciocomprauniTextEdit.Properties.ReadOnly = True
        DescripcionTextEdit.Focus()
        CheckEdit1.Enabled = True
    End Sub
    Private Sub modificar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles modificar.Click
        otro = 2
        AgregarInvetario.Enabled = True
        PrecioVentaCliente.EditValue = 0.0
        PrecioVentaVisita.EditValue = 0.0
        PreciocomprauniTextEdit2.EditValue = 0.0
        Idtipo.EditValue = 0
        ExistenciaTextEdit2.Visible = False
        ExistenciaTextEdit.Visible = True
        PreciocomprauniTextEdit.Visible = True
        PreciocomprauniTextEdit2.Visible = False
        variable = 2
        CheckEdit1.Checked = False
        CheckEdit1.Enabled = False
        AgregarInvetario.Enabled = True
        ExistenciaTextEdit.Properties.ReadOnly = True
        Mostrar()
        IdunidadescategoriaLookUpEdit.Enabled = False
        ExistenciaTextEdit.Properties.ReadOnly = True
        PreciocomprauniTextEdit.Properties.ReadOnly = True

    End Sub
    Private Sub cancelar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cancelar.Click
        If variable <> 2 Then
            Try
                ProductosTableAdapter.DeleteQuery(Val(IdproductoTextEdit.EditValue))
                'AlertControl1.AppearanceCaption.ForeColor = Color.Green
                'AlertControl1.Show(Owner, "Productos", "Se eliminaron los datos correctamente")
                Me.ProductosTableAdapter.Fill(Me.DbFireAndIceDataSet.productos)
            Catch ex As Exception
                'AlertControl1.AppearanceCaption.ForeColor = Color.Red
                'AlertControl1.Show(Owner, "ERROR", "Lo sentimos, el registro no puede ser eliminado ya que está relacionado con otras tablas")

            End Try
        End If
        PrecioVentaCliente.EditValue = 0.0
        PrecioVentaVisita.EditValue = 0.0
        PreciocomprauniTextEdit2.EditValue = 0.0
        Idtipo.EditValue = 0
        ExistenciaTextEdit2.Visible = False
        ExistenciaTextEdit.Visible = True
        PreciocomprauniTextEdit.Visible = True
        PreciocomprauniTextEdit2.Visible = False
        Mostrar()
        Try
            'TODO: This line of code loads data into the 'DbFireAndIceDataSet.productos' table. You can move, or remove it, as needed.
            Me.ProductosTableAdapter.Fill(Me.DbFireAndIceDataSet.productos)
        Catch ex As Exception
            AlertControl1.AppearanceCaption.ForeColor = Color.Red
            AlertControl1.Show(Owner, "ERROR", "No se pudo cargar registros de Productos")

        End Try

        ErrorProvider1.SetError(DescripcionTextEdit, Nothing)
        ErrorProvider1.SetError(IdproductoscategoriaLookUpEdit, Nothing)
        ErrorProvider1.SetError(PreciocomprauniTextEdit, Nothing)
        ErrorProvider1.SetError(IdbodegaLookUpEdit, Nothing)
        ErrorProvider1.SetError(IdunidadescategoriaLookUpEdit, Nothing)

        CheckEdit1.Checked = True
    End Sub
    Private Sub guardar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles guardar.Click
        Dim idd As Integer

        PreciocomprauniTextEdit.Visible = True
        PreciocomprauniTextEdit2.Visible = False
        If DescripcionTextEdit.Text = "" Then
            ErrorProvider1.SetError(DescripcionTextEdit, "Ingrese una Descripción del Producto")
        Else
            ErrorProvider1.SetError(DescripcionTextEdit, Nothing)

            If IdproductoscategoriaLookUpEdit.Text = "" Then
                ErrorProvider1.SetError(IdproductoscategoriaLookUpEdit, "Seleccione una Categoría del Producto")
            Else
                ErrorProvider1.SetError(IdproductoscategoriaLookUpEdit, Nothing)



                If IdbodegaLookUpEdit.Text = "" Then
                    ErrorProvider1.SetError(IdbodegaLookUpEdit, "Seleccione una Bodega para el Producto")
                Else
                    ErrorProvider1.SetError(IdbodegaLookUpEdit, Nothing)

                    If IdunidadescategoriaLookUpEdit.Text = "" Then
                        ErrorProvider1.SetError(IdunidadescategoriaLookUpEdit, "Seleccione una Categoría de Unidad del Producto")
                    Else
                        ErrorProvider1.SetError(IdunidadescategoriaLookUpEdit, Nothing)
                        If otro = 1 Then

                            'PRIMERA CONDICION
                            If CheckEdit1.Checked = True Then

                                If PrecioVentaCliente.EditValue < 1 And PrecioVentaVisita.EditValue < 1 And Idtipo.EditValue < 1 Then
                                    ProductosItem.ShowDialog()
                                Else
                                    Dim opcion As Integer

                                    opcion = MsgBox(String.Format("¿Está seguro que ingresar el {0} con la unidad de medida en {1}?  Advertencia: Recuerde que una vez ingresado el producto con una unidad de medida no se puede cambiar.", DescripcionTextEdit.Text, IdunidadescategoriaLookUpEdit.GetColumnValue("description")), MsgBoxStyle.YesNo + MsgBoxStyle.Question, "Almacenar Producto")
                                    If opcion = vbYes Then


                                        idd = ItemventaTableAdapter.ScalarQueryitem
                                        'MsgBox(ItemventaTableAdapter.ScalarQueryitem)
                                        Try
                                            ProductosBindingNavigatorSaveItem.PerformClick()

                                        Catch ex As Exception

                                        End Try
                                        If PreciocomprauniTextEdit.EditValue Is DBNull.Value Then
                                            PreciocomprauniTextEdit.EditValue = 0
                                        End If

                                        Try
                                            ItemventaTableAdapter.InsertQueryItem(Val(idd), DescripcionTextEdit.Text, DescripcionTextEdit.Text, Val(PreciocomprauniTextEdit.EditValue), Val(PrecioVentaCliente.EditValue), ObservacionTextEdit.Text, Val(Idtipo.EditValue), Val(PrecioVentaVisita.EditValue), False)
                                        Catch ex As Exception
                                            'AlertControl1.AppearanceCaption.ForeColor = Color.Red
                                            'AlertControl1.Show(Owner, "ERROR", "No se pudo almacenar el Producto Venta Encabezado")
                                        End Try

                                        Try
                                            ItemventadetalleTableAdapter.InsertQueryItemdetalle(idd, Val(IdproductoTextEdit.EditValue), 1, Val(PreciocomprauniTextEdit.EditValue), Val(PreciocomprauniTextEdit.EditValue))
                                            Mostrar()
                                            AlertControl1.AppearanceCaption.ForeColor = Color.Green
                                            AlertControl1.Show(Owner, "Producto Venta", "Se Almaceno correctamente el Producto venta")

                                        Catch ex As Exception
                                            Mostrar()
                                            AlertControl1.AppearanceCaption.ForeColor = Color.Red
                                            AlertControl1.Show(Owner, "ERROR", "No se pudo almacenar el Producto Venta Detalle")
                                        End Try

                                    End If

                                End If

                            End If
                            'FIN PRIMERA CONDICION

                            'SEGUNDA CONDICION
                            If CheckEdit1.Checked = False Then
                                Try
                                    Dim opcion As Integer

                                    opcion = MsgBox(String.Format("¿Está seguro que ingresar el {0} con la unidad de medida en {1}?  Nota: Recuerde que una vez ingresado el producto con una unidad de medida no se puede cambiar.", DescripcionTextEdit.Text, IdunidadescategoriaLookUpEdit.GetColumnValue("description")), MsgBoxStyle.YesNo + MsgBoxStyle.Question, "Almacenar Producto")
                                    If opcion = vbYes Then
                                        ProductosBindingNavigatorSaveItem.PerformClick()
                                        AlertControl1.AppearanceCaption.ForeColor = Color.Green
                                        AlertControl1.Show(Owner, "Producto Venta", "Se Almaceno correctamente el Producto venta")

                                        Mostrar()
                                    End If
                                Catch ex As Exception

                                End Try
                            End If
                            'FIN SEGUNDA CONDICION
                        End If


                        If otro = 2 Then
                            'PRIMERA CONDICION
                            If CheckEdit1.Checked = True Then

                                If PrecioVentaCliente.EditValue < 1 And PrecioVentaVisita.EditValue < 1 And Idtipo.EditValue < 1 Then
                                    ProductosItem.ShowDialog()
                                Else

                                    idd = ItemventaTableAdapter.ScalarQueryitem
                                    'MsgBox(ItemventaTableAdapter.ScalarQueryitem)
                                    Try
                                        ProductosBindingNavigatorSaveItem.PerformClick()

                                    Catch ex As Exception

                                    End Try
                                    If PreciocomprauniTextEdit.EditValue Is DBNull.Value Then
                                        PreciocomprauniTextEdit.EditValue = 0
                                    End If

                                    Try
                                        ItemventaTableAdapter.InsertQueryItem(Val(idd), DescripcionTextEdit.Text, DescripcionTextEdit.Text, Val(PreciocomprauniTextEdit.EditValue), Val(PrecioVentaCliente.EditValue), Val(IdunidadescategoriaLookUpEdit.EditValue), Val(Idtipo.EditValue), Val(PrecioVentaVisita.EditValue), False)
                                    Catch ex As Exception
                                        'AlertControl1.AppearanceCaption.ForeColor = Color.Red
                                        'AlertControl1.Show(Owner, "ERROR", "No se pudo almacenar el Producto Venta Encabezado")
                                    End Try

                                    Try
                                        ItemventadetalleTableAdapter.InsertQueryItemdetalle(idd, Val(IdproductoTextEdit.EditValue), 1, Val(PreciocomprauniTextEdit.EditValue), Val(PreciocomprauniTextEdit.EditValue))
                                        Mostrar()
                                        AlertControl1.AppearanceCaption.ForeColor = Color.Green
                                        AlertControl1.Show(Owner, "Producto Venta", "Se Almaceno correctamente el Producto venta")

                                    Catch ex As Exception
                                        Mostrar()
                                        AlertControl1.AppearanceCaption.ForeColor = Color.Red
                                        AlertControl1.Show(Owner, "ERROR", "No se pudo almacenar el Producto Venta Detalle")
                                    End Try

                                End If

                            End If
                            'FIN PRIMERA CONDICION

                            'SEGUNDA CONDICION
                            If CheckEdit1.Checked = False Then
                                Try
                                    ProductosBindingNavigatorSaveItem.PerformClick()
                                    AlertControl1.AppearanceCaption.ForeColor = Color.Green
                                    AlertControl1.Show(Owner, "Producto Venta", "Se Almaceno correctamente el Producto venta")

                                    Mostrar()
                                Catch ex As Exception

                                End Try
                            End If
                            'FIN SEGUNDA CONDICION
                        End If
                    End If

                End If
            End If
        End If
        ExistenciaTextEdit2.Visible = False
        PreciocomprauniTextEdit2.Visible = False

        ExistenciaTextEdit.Visible = True
        PreciocomprauniTextEdit.Visible = True
    End Sub
    Private Sub SimpleButton6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        AgregarInventario.ShowDialog()
    End Sub
    Private Sub eliminar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles eliminar.Click

        Dim opcion As Integer

        opcion = MsgBox("¿Está seguro que desea eliminar el registro?", MsgBoxStyle.YesNo + MsgBoxStyle.Question, "Eliminar registro")
        If opcion = vbYes Then
            Try
                ProductosTableAdapter.DeleteQuery(Val(IdproductoTextEdit.EditValue))
                AlertControl1.AppearanceCaption.ForeColor = Color.Green
                AlertControl1.Show(Owner, "Productos", "Se eliminaron los datos correctamente")
                Me.ProductosTableAdapter.Fill(Me.DbFireAndIceDataSet.productos)
            Catch ex As Exception
                AlertControl1.AppearanceCaption.ForeColor = Color.Red
                AlertControl1.Show(Owner, "ERROR", "Lo sentimos, el registro no puede ser eliminado ya que está relacionado con otras tablas")

            End Try
        End If
        ExistenciaTextEdit2.Visible = False
        ExistenciaTextEdit.Visible = True
        PreciocomprauniTextEdit.Visible = True
        PreciocomprauniTextEdit2.Visible = False
    End Sub
    Private Sub imprimir_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles imprimir.Click
        GridControl1.ShowPrintPreview()
    End Sub
    Private Sub SimpleButton6_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AgregarInvetario.Click
        AgregarInventario.ShowDialog()

    End Sub

    Private Sub SimpleButton6_Click_2(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SimpleButton6.Click
        My.Forms.CategoriaProductos.Close()
        CategoriaProductos.ShowDialog()

        Try
            ProductoscategoriaTableAdapter.Fill(DbFireAndIceDataSet.productoscategoria)

        Catch ex As Exception
            AlertControl1.AppearanceCaption.ForeColor = Color.Red
            AlertControl1.Show(Owner, "ERROR", "No se pudieron cargar los datos")
        End Try
    End Sub

    Private Sub TextEdit1_EditValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextEdit1.EditValueChanged
        If TextEdit1.GetColumnValue("equivalenciauref") Is DBNull.Value Then
            equivalencia = 1
        Else
            If PreciocomprauniTextEdit.EditValue Is DBNull.Value Then
                PreciocomprauniTextEdit.EditValue = 0.0
            End If
            If PreciocomprauniTextEdit2.EditValue Is DBNull.Value Then
                PreciocomprauniTextEdit2.EditValue = 0.0
            End If
            Try

                equivalencia = TextEdit1.GetColumnValue("equivalenciauref")
                ExistenciaTextEdit2.Visible = True
                ExistenciaTextEdit.Visible = False
                PreciocomprauniTextEdit.Visible = False
                PreciocomprauniTextEdit2.Visible = True
                ExistenciaTextEdit2.EditValue = ExistenciaTextEdit.EditValue / equivalencia
                PreciocomprauniTextEdit2.EditValue = PreciocomprauniTextEdit.EditValue / equivalencia
            Catch ex As Exception

            End Try
        End If

    End Sub

    Private Sub IdproductoTextEdit_EditValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles IdproductoTextEdit.EditValueChanged

    End Sub

    Private Sub IdunidadescategoriaLookUpEdit_EditValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles IdunidadescategoriaLookUpEdit.EditValueChanged
        If IdunidadescategoriaLookUpEdit.EditValue Is DBNull.Value Then
        Else
            Dim id As Integer = IdunidadescategoriaLookUpEdit.EditValue
            Try

                UnidadesdemedidaTableAdapter.FillByUnidades(DbFireAndIceDataSet.unidadesdemedida, id)
            Catch ex As Exception
                AlertControl1.AppearanceCaption.ForeColor = Color.Red
                AlertControl1.Show(Owner, "ERROR", "No se pudieron cargar los datos")
            End Try
        End If
        Label3.Text = IdunidadescategoriaLookUpEdit.GetColumnValue("description")

    End Sub
End Class